﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using W.Models.DataAccess;
using System.Linq.Expressions;
using W.Models.Utils;
using System.Data.Objects;

namespace W.Component
{
    public abstract class RecursosComponent<TEntity> : ComponentBase<Recursos> where TEntity : Recursos
    {
        protected List<TEntity> Get(Expression<Func<Recursos, bool>> r)
        {
            List<Recursos> CollectionBase = this.Model.Recursos.IncludeAll().Where(r).ToList().FindAll(t => t.GetType() == typeof(TEntity));
            List<TEntity> Collection = CollectionBase.ConvertAll<TEntity>(t => (TEntity)(object)t);

            return Collection;
        }

        public List<TEntity> Get()
        {
            List<Recursos> CollectionBase = this.Model.Recursos.IncludeAll().Where(t => t.EstadosRecurso.id != 3).ToList().FindAll(t => t.GetType() == typeof(TEntity));
            List<TEntity> Collection = CollectionBase.ConvertAll<TEntity>(t => (TEntity)(object)t);

            return Collection;
        }

        public override void Create(Recursos Entity)
        {
            Entity.EstadosRecurso = this.Get<EstadosRecurso>(Entity.EstadosRecurso.id);

            base.Create(Entity);
        }

        public override void Delete(int id)
        {
            Recursos entity = Get(id);
            entity.EstadosRecurso = Model.EstadosRecurso.Single(t=>t.id == 3);
            Update(entity);
        }
    }
}
